// lookdriverposition.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    marks: [],
    lines: [],
    phoneHeight: wx.getSystemInfoSync().windowHeight,
    names: '张师傅',
    timer: '',
    drivermap: '',
    Latitude: '',
    Longitude: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    try {
      this.drivermap = wx.createMapContext('drivermap');
      this.setData({
        names: options.name,
        ordercode: options.ordercode
      });
      this.init();
      var tmp = setInterval(this.refreshlocation, 5000);
      this.data.timer = tmp;
      this.getMyLocation();
      this.getDriverLocation();
    } catch (e) {
      console.log(e);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    clearInterval(this.data.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    clearInterval(this.data.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    if (res.from === 'menu') {
      return app.OnShare();
    }
  },
  init: function() {
    var m = [{
      iconPath: "../../../pages/image/location.png",
      id: 0,
      latitude: '',
      longitude: '',
      width: 30,
      height: 30,
      callout: {
        content: '我',
        bgColor: '#ff8208',
        color: '#ffffff',
        borderRadius: 5,
        padding: 5,
        display: "ALWAYS"
      }
    }, {
      iconPath: "../../../pages/image/location.png",
      id: 1,
      latitude: '',
      longitude: '',
      width: 30,
      height: 30,
      callout: {
        content: '司机',
        bgColor: '#ff8208',
        color: '#ffffff',
        borderRadius: 5,
        padding: 5,
        display: "ALWAYS"
      }
    }];
    var line = [{
      points: [{
        latitude: '',
        longitude: '',
      }, {
        latitude: '',
        longitude: '',
      }],
      color: "#ff8208",
      width: 2,
      dottedLine: true
    }];
    var control = [{
      id: 1,
      iconPath: '../../../pages/image/refresh.png',
      position: {
        left: 10,
        top: this.data.phoneHeight - 60,
        width: 30,
        height: 30,
      },
      clickable: true
    }];
    this.setData({
      marks: m,
      lines: line,
      controls: control
    });
  },
  /*获取个人位置信息*/
  getMyLocation: function() {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function(res) {
        try {
          that.data.Latitude = res.latitude;
          that.data.Longitude = res.longitude;
          var tmp = that.data.marks;
          tmp[0].latitude = res.latitude;
          tmp[0].longitude = res.longitude;

          var linedata = that.data.lines;
          linedata[0].points[0].latitude = res.latitude;
          linedata[0].points[0].longitude = res.longitude;
          that.setData({
            marks: tmp,
            lines: linedata,
            Latitude: res.latitude,
            Longitude: res.longitude
          });
          that.showmarks();
        } catch (a) {
          console.log(a);
        };
      },
    });
  },
  /*获取司机位置信息*/
  getDriverLocation: function() {
    var that = this;
    wx.request({
      url: app.serviceurl + 'WxMicro/YingYanSearch',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      data: {
        OrderCode: that.data.ordercode,
        PlateNumber: app.PlateNumber,
        Latitude: that.data.Latitude,
        Longitude: that.data.Longitude
      },
      dataType: 'json',
      success: function(res) {
        if (!res.data.Result && res.data.Locations.length == 0) {
          wx.showModal({
            content: res.data.Message,
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                wx.navigateBack({});
              }
            }
          });
          return
        }
        var tmp = that.data.marks;
        tmp[1].latitude = res.data.Locations[0].Latitude;
        tmp[1].longitude = res.data.Locations[0].Longitude;

        var linedata = that.data.lines;
        linedata[0].points[1].latitude = res.data.Locations[0].Latitude;
        linedata[0].points[1].longitude = res.data.Locations[0].Longitude;
        that.setData({
          marks: tmp,
          lines: linedata
        });
        that.showmarks();
      },
      fail: function(xhr) {
        console.log("司机位置出错");
        console.log(xhr);
      }
    })
  },
  showmarks: function() {
    var pointsinfo = this.data.lines[0].points;
    this.drivermap.includePoints({
      padding: [100, 100, 100, 100],
      points: pointsinfo
    });
  },
  refreshlocation: function() {
    this.getDriverLocation();
  },
  controltap: function() {
    this.getDriverLocation();
    this.getMyLocation();
  }
})